import time
from Sentry3 import *
from mpython import *
from machine import UART


uart1 = UART(1, baudrate=57600, tx=Pin.P14, rx=Pin.P13)

Sentry3 = Sentry3(0x60)

# 自定义函数
def UART():
  global my_variable, row, column, x, y, i, j, index, flag, num, param_set
  Sentry3.begin(uart1)
def I2C():
  global my_variable, row, column, x, y, i, j, index, flag, num, param_set
  Sentry3.begin(i2c)
def Result_Display():
  global my_variable, row, column, x, y, i, j, index, flag, num, param_set
  # Sentry3不主动返回检测识别结果，需要主控板发送指令进行读取。读取的流程：首先读取识别结果的数量，接收到指令后，Sentry3会刷新结果数据，如果结果数量不为零，那么主控再发送指令读取结果的相关信息。请务必按此流程构建程序。
  num = (Sentry3.GetValue(sentry3_vision_e.kVisionColor, sentry_obj_info_e.kStatus))
  i = 1
  for index in range(num):
    if (i < 10):
      oled.DispChar((str("Point#") + str(((str(i))[0:1]))), 0, (2-1)*16, 1)
    else:
      oled.DispChar((str("Point#") + str(((str(i))[0:2]))), 0, (2-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionColor,sentry_obj_info_e.kLabel,i)==color_label_e.kColorBlack):
      oled.DispChar("Black", 0, (3-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionColor,sentry_obj_info_e.kLabel,i)==color_label_e.kColorWhite):
      oled.DispChar("White", 0, (3-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionColor,sentry_obj_info_e.kLabel,i)==color_label_e.kColorRed):
      oled.DispChar("Red", 0, (3-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionColor,sentry_obj_info_e.kLabel,i)==color_label_e.kColorGreen):
      oled.DispChar("Green", 0, (3-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionColor,sentry_obj_info_e.kLabel,i)==color_label_e.kColorBlue):
      oled.DispChar("Blue", 0, (3-1)*16, 1)
    if (Sentry3.GetValue(sentry3_vision_e.kVisionColor,sentry_obj_info_e.kLabel,i)==color_label_e.kColorYellow):
      oled.DispChar("Yellow", 0, (3-1)*16, 1)
    oled.show()
    time.sleep(0.2)
    oled.fill_rect(0, (2-1)*16, 128, 16, 0)
    oled.fill_rect(0, (3-1)*16, 128, 16, 0)
    oled.show()
    i = (i + 1)
def Flag1():
  global my_variable, row, column, x, y, i, j, index, flag, num, param_set
  Sentry3.SetParamNum(sentry3_vision_e.kVisionColor,2)
  Sentry3.SetParam(sentry3_vision_e.kVisionColor,[25, 25, 8, 8, 0],1)
  Sentry3.SetParam(sentry3_vision_e.kVisionColor,[75, 75, 9, 12, 0],2)
  # 等待参数生效
  time.sleep(0.1)
def Flag2():
  global my_variable, row, column, x, y, i, j, index, flag, num, param_set
  Sentry3.SetParamNum(sentry3_vision_e.kVisionColor,(row * column))
  # 切换行
  for i in range(1, (row + 1), 1):
    y = (i * (round((100 / (row + 1)))))
    # 切换列
    for j in range(1, (column + 1), 1):
      x = (j * (round((100 / (column + 1)))))
      index = (j + (row * (i - 1)))
      # 设置25组参数
      Sentry3.SetParam(sentry3_vision_e.kVisionColor,[x, y, 6, 8, 0],index)
      time.sleep(0.1)

# 事件回调函数
def on_button_a_down(_):
  time.sleep_ms(10)
  if button_a.value() == 1: return
  global param_set
  global flag
  param_set = 1
  flag = (flag + 1)


button_a.irq(trigger=Pin.IRQ_FALLING, handler=on_button_a_down)
param_set = 0
flag = 0
index = 0
# 设置识别点的总行数，根据需要自行修改
row = 5
# 设置识别点的总列数，根据需要自行修改
column = 5
x = 0
y = 0
i = 0
j = 0
num = 0
# 等待Sentry3完成操作系统的初始化。此等待时间不可去掉，避免出现Sentry3尚未初始化完毕主控器已经开发发送指令的情况
time.sleep(2)
I2C()
# 算法的默认参数，如果不用更改，则可以去掉本代码块
Sentry3.SetParamNum(sentry3_vision_e.kVisionColor,1)
# 算法的默认参数，如果不用更改，则可以去掉本代码块
Sentry3.SetParam(sentry3_vision_e.kVisionColor,[50, 50, 3, 4, 0],1)
Sentry3.VisionBegin(sentry3_vision_e.kVisionColor)
oled.DispChar("Algo: Color", 0, (1-1)*16, 1)
oled.show()
while True:
  if (param_set == 1):
    param_set = 0
    if (flag == 1):
      Flag1()
    else:
      Flag2()
      flag = 0
    oled.DispChar("Operation Done!", 0, (4-1)*16, 1)
    oled.show()
    time.sleep(0.5)
    oled.fill_rect(0, (4-1)*16, 128, 16, 0)
    oled.show()
  Result_Display()
